package br.com.news.application;

import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import br.com.news.application.buttons.CategoriaButton;
import br.com.news.application.enumerations.ButtonTypeEnum;
import br.com.news.application.interfaces.ICategoriaData;
import br.com.news.application.interfaces.ICategoriaGrid;
import br.com.news.utils.database.beans.CategoriaBean;
import br.news.application.utils.interfaces.IApplicationServices;

@Named
public class CategoriaGridPanel extends JPanel implements ICategoriaGrid, ICategoriaData {

	private static final long serialVersionUID = -3127985955124406559L;

	private final Map<Long, CategoriaBean> categoriaContext = new HashMap<Long, CategoriaBean>();

	private JTable table;
	private DefaultTableModel tableModel;
	private JTextField txtCategoriaId;
	private JTextField txtCategoriaDescricao;
	private JCheckBox chkCategoriaStatus;
	private CategoriaButton btnInsert;
	private CategoriaButton btnUpdate;
	private CategoriaButton btnDelete;
	private CategoriaButton btnFind;

	private IApplicationServices services;

	/**
	 * Create the panel.
	 */
	public CategoriaGridPanel() {
		String[] columnNames = new String []{ "ID", "Nome", "Descri��o"};

		this.tableModel = new DefaultTableModel(columnNames, 0);
		setLayout(null);

		this.txtCategoriaId = new JTextField();
		this.txtCategoriaId.setEditable(false);
		this.txtCategoriaId.setBounds(108, 14, 86, 20);
		this.add(this.txtCategoriaId);
		this.txtCategoriaId.setColumns(10);

		this.table = new JTable(this.tableModel) {

			private static final long serialVersionUID = -7781956365105743313L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}

			@Override
			public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
				Object categoryId = CategoriaGridPanel.this.table.getValueAt(rowIndex, 0);

				if (categoryId != null) {
					CategoriaBean categoria = CategoriaGridPanel.this.categoriaContext.get(new Long(categoryId.toString()));

					if (categoria != null) {
						CategoriaGridPanel.this.txtCategoriaId.setText(categoria.getIdCategoria().toString());
						CategoriaGridPanel.this.chkCategoriaStatus.setSelected(categoria.isStatus());
						CategoriaGridPanel.this.txtCategoriaDescricao.setText(categoria.getDescricao());
					}
				}

				super.changeSelection(rowIndex, columnIndex, toggle, extend);
			}

		};
		this.table.setBounds(0, 147, 600, 253);
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

		this.add(this.table);
		this.add(this.table.getTableHeader(), BorderLayout.NORTH);

		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 17, 79, 14);
		this.add(lblId);

		JLabel lblDescription = new JLabel("Descrição:");
		lblDescription.setBounds(10, 85, 79, 14);
		this.add(lblDescription);

		JLabel lblCategoriaStatus = new JLabel("Nome:");
		lblCategoriaStatus.setBounds(10, 51, 79, 14);
		this.add(lblCategoriaStatus);

		this.txtCategoriaDescricao = new JTextField();
		this.txtCategoriaDescricao.setBounds(108, 82, 383, 20);
		this.add(this.txtCategoriaDescricao);
		this.txtCategoriaDescricao.setColumns(10);

		this.btnInsert = new CategoriaButton(ButtonTypeEnum.INSERT, this, this);
		this.btnInsert.setBounds(501, 11, 89, 23);
		this.add(this.btnInsert);

		this.btnUpdate = new CategoriaButton(ButtonTypeEnum.UPDATE, this, this);
		this.btnUpdate.setBounds(501, 45, 89, 23);
		this.add(this.btnUpdate);

		this.btnDelete = new CategoriaButton(ButtonTypeEnum.DELETE, this, this);
		this.btnDelete.setBounds(501, 79, 89, 23);
		this.add(this.btnDelete);

		this.btnFind = new CategoriaButton(ButtonTypeEnum.FIND, this, this);
		this.btnFind.setBounds(501, 113, 89, 23);
		this.add(this.btnFind);

		this.chkCategoriaStatus = new JCheckBox("Status");
		this.chkCategoriaStatus.setBounds(107, 47, 97, 23);
		add(this.chkCategoriaStatus);
	}

	public void initialize(IApplicationServices services) {
		this.services = services;

		this.btnFind.doClick();
	}

	@Override
	public void addCategories(List<CategoriaBean> categorias) {
		while (this.tableModel.getRowCount()>0){
			this.tableModel.removeRow(0);
		}

		this.categoriaContext.clear();

		for (CategoriaBean categoria : categorias) {
			addCategoriaRow(categoria);
		}
	}

	private void addCategoriaRow(CategoriaBean categoria) {
		this.categoriaContext.put(categoria.getIdCategoria(), categoria);

		this.tableModel.addRow(new Object[] {
				categoria.getIdCategoria(),
				categoria.isStatus(),
				categoria.getDescricao() });
	}

	@Override
	public void clearFields() {
		this.txtCategoriaId.setText("");
		this.txtCategoriaDescricao.setText("");
		this.chkCategoriaStatus.setSelected(false);
	}

	@Override
	public Long getCategoriaId() {
		String categoriaId = this.txtCategoriaId.getText();

		if ((categoriaId == null) || categoriaId.trim().isEmpty()) {
			return null;
		}

		return new Long(categoriaId);
	}

	@Override
	public boolean getCategoriaStatus() {
		return this.chkCategoriaStatus.isSelected();
	}

	@Override
	public String getCategoriadescricao() {
		return this.txtCategoriaDescricao.getText();
	}


	@Override
	public IApplicationServices getService() {
		return this.services;
	}
}



